//
//  ArriveStoreViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/4/13.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire

class ArriveStoreViewController: BaseViewController {
    
    @IBOutlet var carInfoTF: UITextField!
    
    @IBOutlet var nameTF: UITextField!
    
    @IBOutlet var phoneTF: UITextField!
    
    @IBOutlet var serviceBtn: UIButton!
    
    @IBOutlet var remarksTF: UITextField!
    
    @IBOutlet var submitBtn: UIButton!
    
    var service :String?
    
    var storeServices : [ServiceModel]?
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.view.backgroundColor = UIColor.white
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        requstStoreConfigList()
    }

//按钮点击事件
    
    @IBAction func serviceBtnPress(_ sender: UIButton) {
        self.view.endEditing(true)
        var Ids = [String]()
        var Names = [String]()
        for (service) in storeServices! {
            
            Names.append(service.name)
            Ids.append(service.id)
        }
        //维修  改装 保养 洗车 掌控安装
        BRStringPickerView.showStringPicker(withTitle: "服务类型", dataSource: Names, defaultSelValue: Names[0], isAutoSelect: false) { (str,index) in
            self.serviceBtn.setTitle(str as? String, for: .normal)
            self.service = Ids[index]
        }
        
    }
    
    @IBAction func submitBtnPress(_ sender: UIButton) {

        if (carInfoTF.text == "") || (self.nameTF.text == "") || (self.phoneTF.text == "") || (self.remarksTF.text == "") || (service == nil) {
            MBProgressHUD.alertShow(in: self.view, message: "请填写完整再提交", hidenAfter: 0.8, mode: MBProgressHUDMode.text)
            return
        }
            
        requestSumbitArriveservice(carInfo: carInfoTF.text!, name: nameTF.text!, phone: phoneTF.text!, serviceType:self.service!, remarks: remarksTF.text!)
    }

//网络请求
    //提交到店服务
    func requestSumbitArriveservice(carInfo:String,name:String,phone:String,serviceType:String,remarks:String) -> Void {
        
        let param =
            ["car":carInfo,
             "username":name,
             "phone":phone,
             "description":remarks,
             "service_type":serviceType,
             "storeid":UserModel.shareUser.data?.storeId  ?? String()] as [String : Any]
        
        Alamofire.request(addArriveStoreUrl, method:.post, parameters:param)
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    
                    var basemodel = DefaultData()
                    basemodel = DefaultData.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        print("数据请求成功")
                        self.navigationController?.popViewController(animated: true)

                    case false:
                         MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                    
                case false:
                    print(response.result.error!)
                }
                
        }
    }
    
    //店铺配置列表
    func requstStoreConfigList() -> Void {
        
        Alamofire.request(storeConfigListUrl, method:.post, parameters: ["sid":UserModel.shareUser.data?.storeId ?? String()])
            .responseString { response in
            
                switch response.result.isSuccess {
                case true:
                    
                    var basemodel = BaseResponseOfModel<BaseArrayData<ServiceModel>>()
                    basemodel = BaseResponseOfModel.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        
                        self.storeServices = basemodel.data?.data
                        print("数据请求成功")

                    case false:
                         MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                        
                    }
                case false:
                    print(response.result.error!)
                }
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
